<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/11/10
 * Time: 15:07
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\admin\controller;

use app\common\business\User as BusObj;
use app\common\lib\Show;
use app\common\lib\Status;
use think\App;
use think\Exception;

class User extends Base
{
    protected BusObj $busObj;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->busObj = new BusObj();
    }
    public function index()
    {
        $this->indexWhere[] = [
            ['id','<>',1]
        ];
        return parent::index(); // TODO: Change the autogenerated stub
    }

    public function edit()
    {
        $eid = $this->request->param('eid',0,'intval');
        if($eid === 1){
            throw new Exception("不允许修改");
        }
        return parent::edit(); // TODO: Change the autogenerated stub
    }
    public function editSave(){
        $data = $this->request->param();
        if(empty($data['move_pwd'])){
            unset($data['move_pwd']);
        }else{
            $data['code'] = md5(random_bytes(32));
            $data['move_pwd'] = md5($data['move_pwd'].$data['code']);
        }
        if(empty($data['move_pwd_res'])){
            unset($data['move_pwd_res']);
        }else{
            if(empty($data['move_pwd'])){
                return Show::json_show(Status::status_error,"请输入密码");
            }
            $data['move_pwd_res'] = md5($data['move_pwd_res'].$data['code']);
            if($data['move_pwd_res'] != $data['move_pwd']){
                return Show::json_show(Status::status_error,"两次密码不一致");
            }
        }
        $res = $this->busObj->editSave($data);
        if($res){
            return Show::json_show(Status::status_normal,"ok");
        }else{
            return Show::json_show(Status::status_error,"操作失败");
        }
    }
}
